package net.roobook.cms.modules.admin.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import net.roobook.cms.common.constant.BaseConstant;
import net.roobook.cms.common.entity.ResResult;
import net.roobook.cms.common.entity.StateCodeEnum;
import net.roobook.cms.common.utils.DateUtils;
import net.roobook.cms.common.utils.FileConfiguration;
import net.roobook.cms.common.web.BaseController;
import net.roobook.cms.modules.admin.entity.SysSetting;
import net.roobook.cms.modules.admin.service.SysSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @author cos
 * @email 327878610@qq.com
 * @link https://roobook.net
 * @Description 上传控制器
 * @date 2023/10/11 8:33
 **/
@Controller
@Scope("prototype")
@RequestMapping("upload")
public class UploadController extends BaseController {
    @Autowired
    private FileConfiguration fileConfiguration;
    @Autowired
    private SysSettingService sysSettingService;
    /**
     *   文件上传
     * @param file
     */
    @RequestMapping("uploadFile")
    public void upload(@RequestParam("file") MultipartFile file) {
        ResResult respResult = null;
        JSONObject result = new JSONObject();
        try {
            if(file.isEmpty()) {
                return;
            }
            String rootPath = fileConfiguration.getResourceDir();
            String currentDate = DateUtils.getCurrentDate("yyyyMMdd");
            SysSetting system = sysSettingService.getSystem();
            String uploadDir = system.getUploaddir();
            File directory  = new File(rootPath + "/" + uploadDir + "/" + currentDate);
            if(!directory.exists()){
                directory.mkdirs();
            }
            String newFileName = IdUtil.getSnowflakeNextIdStr() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String absolutePath = directory.getAbsolutePath(); //获取绝对路径
            File uploadpath = new File(absolutePath + "/" + newFileName);
            file.transferTo(uploadpath);
            result.put("filepath", currentDate + "/" + newFileName);
            result.put("name", newFileName);
            result.put("originalFilename", file.getOriginalFilename());
            result.put("filesize", file.getSize());
            result.put("filetype", file.getContentType());
            result.put("url", system.getWebsite() + BaseConstant.UPLOAD_PREFIX + uploadDir + "/" + currentDate + "/" + newFileName);
            respResult = ResResult.Factory.newInstance(Boolean.TRUE,
                    StateCodeEnum.HTTP_SUCCESS.getCode(), result,
                    StateCodeEnum.HTTP_SUCCESS.getDescription());
        } catch (Exception e) {
            respResult = ResResult.Factory.newInstance(Boolean.TRUE,
                    StateCodeEnum.HTTP_ERROR.getCode(), null,
                    StateCodeEnum.HTTP_ERROR.getDescription());
            e.printStackTrace();
        }
        this.outJson(respResult);
    }

}
